Contextmenu (Library) |
Contextmenu
Creates a shortcut menu with entries defined using menu items.
Syntax
HTML |
<div cordysType ="wcp.library.ui.ContextMenu id="contextmenuID" > |
where contextmenuID
is the string that specifies the unique ID for the shortcut menu. This identifier must be unique within the page.
After shortcut menus are defined, menu items can be defined inside them using the following syntax:
HTML |
<div id="mID" |
where mID
denotes the unique ID of the menu item inside the shortcut menu, and content denotes the actual content of the menu item that is displayed.
After a shortcut menu is defined, it can be attached to any element by assigning the name of the shortcut menu to the contextmenu
attribute as follows:
HTML |
<element contextmenu=contextMenuID>...</element> |
where element
can be any valid HTML element.
To dynamically add and initialize this component, you can use the initializeHTMLElements or addType methods of the Application object.
The properties, methods, and events defined for this component are as follows:
Table 1. List of Attributes
Attribute |
Property |
Description |
---|---|---|
activeElement |
activeElement |
Object that denotes the HTML element for which the shortcut menu is attached |
appearance |
appearance |
String, optional. Determines the position in which the context menu is displayed. The available options are:
|
automaticLoad |
automaticLoad |
Boolean that denotes the context menu is automatically loaded when the page is loaded
|
description |
|
The attribute is applicable to sticky context menus that can be dragged across an application and display a description and a close icon in the titlebar. |
stickable |
stickable |
Boolean that denotes whether a menu can be made sticky. A sticky menu can be dragged to re-position it on a page and is not hidden when it loses focus. |
sticky |
sticky |
Boolean that denotes whether a menu will remain visible even when it loses focus. The default value is false. |
Table 2. List of Methods
Method |
Description |
---|---|
Adds a new menu item to the context menu and returns the created item |
|
Adds a separator to the context menu and returns the object that is created
|
|
Hides a menu item or a separator in the context menu |
|
Registers an HTML element to the shortcut menu so that the shortcut menu is attached to that element. It takes a parameter |
|
Removes all menu items and separators from the context menu |
|
Removes a menu item or separator from the context menu |
|
Shows a shortcut menu for the event fired |
|
Displays a hidden menu item or separator in the context menu |
|
Removes the context menu attached to an HTML element. It takes a parameter |
|
updateContent( id, content ) | Updates the menu item content |
Table 3. List of Events
Event |
Description |
---|---|
Fired on right-clicking the element to which the shortcut menu is attached |
Once the shortcut menus are defined, menu items can be added to it (statically and dynamically). The menu items created can also have images to represent them. Multi-level shortcut menus are also supported on the menu items. Following are the properties, method and events applied on the menu items.
Table 4. List of Attributes (applicable to menu items)
Attribute/Property |
Description |
---|---|
checkable |
Optional. Boolean that denotes whether the menu item can be checked. |
checked |
Optional. Boolean that denotes whether the menu item is selected. Used in combination with |
Boolean that denotes whether the menu item is enabled or disabled |
|
hidden |
Optional. Boolean that denotes whether the item is displayed or hidden. If set as true, the item is hidden. |
image |
String that specifies the URL of the image to be shown for the menu item |
String that denotes whether the menu item is used as a separator |
|
shortcut |
Optional. String that denotes the description of a menu item. This description is shown at the right hand side of the menu item. A |
String that specifies the unique ID of a shortcut menu, which can be used as a sub-menu for the menu item. |
|
radioname |
String, optional. The menu item is shown with a radio button. |
Table 5. List of Events (applicable to menu items)
Event |
|
Description |
---|---|---|
|
DEPRECATED. To ensure multi-browser support, use |
|
onmenuitemclick |
|
Fires when a menu item is clicked |
Note: When the shortcut menu is loaded in the HTML page and if the content of the context menu is inside the body of the HTML, then the menu item texts will be displayed at the top of the page while loading the page. To avoid this, it is advised to set the display
attribute of the style
property to none. The shortcut menu will automatically set it to visible after the onafterload
event is fired.
Example
The following sample code shows the implementation and use of the contextmenu
behavior:
<\!DOCTYPE HTML PUBLIC"-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Demo of contextmenu</title> <script type="text/javascript" src="/cordys/wcp/application.js"></script> <script type="text/javascript"> function showContext() { application.setStatus("Saving the document..."); } function checker() { var eventObject = window.application.event; window.status = cordys.getTextContent(eventObject.menuitem); } </script> </head> <body class="light" bottommargin=20 leftmargin=20 rightmargin=20 topmargin=20> <h3>Demo of contextmenu</h3> <div contextmenu="contextdemo">Right Click to show contextmenu</div> <div cordysType="wcp.library.ui.ContextMenu" id="contextdemo" style="display:none"> <div onclick="application.notify('Hello')" submenu="newMenu">New</div> <div separator="true"> </div> <div image="/cordys/wcp/theme/default/icon/action/save.png"" onclick="showContext()" shortcut="Ctrl+S">2.Save</div> <div separator="true"> </div> <div radioname="group1" onclick="checker()">Radio Item1</div> <div radioname="group1" onclick="checker()" checked="true">Radio Item2</div> <div separator="true"> </div> <div submenu="myContext">Stickable menu</div> </div> <div cordysType="wcp.library.ui.ContextMenu" id="newMenu" style="display:none"> <div image="/cordys/wcp/theme/default/icon/document/businessprocess.png" onclick="application.notify('New Business Process.')">Process</div> <div image="/cordys/wcp/theme/default/icon/document/rule.png" enabled="false" onclick="application.notify('New Rule.')">Rule</div> <div image="/cordys/wcp/theme/default/icon/document/form.png" onclick="application.notify('New Form.')">Form</div> </div> <div cordysType="wcp.library.ui.ContextMenu" id="myContext" style="display:none" stickable="true"> <div checkable="true" checked="true" onclick="checker()">Checker</div> <div enabled="false">Disabled menu item</div> <div separator="true"> </div> <div onclick="application.notify('Hello.')">Say Hello</div> </div> </body> </html>